import { Component, forwardRef, Input, OnChanges, SimpleChanges } from "@angular/core";
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
import { MonkFormDesign } from "../../monk-form-design.interface";

@Component({
  selector: 'app-design-form-item-radio',
  templateUrl: './design-form-item-radio.component.html',
  styleUrls: ['./design-form-item-radio.component.scss'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => DesignFormItemRadioComponent),
      multi: true
    }
  ]

})
export class DesignFormItemRadioComponent implements ControlValueAccessor, OnChanges {  
  @Input() record//: MonkFormDesign.RadioType;
  onChange: (_: any) => void = (_: any) => { };
  onTouched: () => void = () => { };

  value;
  isDisabled = false;
  writeValue(value: any): void {
    this.value = value;
  }
  registerOnChange(fn: any): void {
    this.onChange = fn
  }
  registerOnTouched(fn: any): void {
    this.onTouched = fn;
  }
  setDisabledState?(isDisabled: boolean): void {
    this.isDisabled = isDisabled;
  }

  ngOnChanges(changes: SimpleChanges): void {
  }

  onModelChange(e) {
    this.value = e;
    this.onTouched()
    this.onChange(e)
    //console.log(e)
  }
}